// 生成新的时间字符串，精确到分钟
function getMinuteTime(timeStr) {
  if (!timeStr || timeStr.length == 0) {
    return timeStr;
  }
  let date = new Date(timeStr);

  let year = date.getFullYear();
  let month = date.getMonth() + 1; // 注意月份从0开始，所以这里要+1
  let day = date.getDate();
  let hour = date.getHours();
  let minute = date.getMinutes();

  let newTimeStr =
    year +
    "/" +
    (month < 10 ? "0" + month : month) +
    "/" +
    (day < 10 ? "0" + day : day) +
    " " +
    (hour < 10 ? "0" + hour : hour) +
    ":" +
    (minute < 10 ? "0" + minute : minute);

  return newTimeStr;
}
// 参数对象转字符串
function serializeQueryParams(params) {
  const query = Object.keys(params)
    .map(
      (key) => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`
    )
    .join("&");
  return `?${query}`;
}

export const throttle = function (fn, wait = 500, isImmediate = false) {
  var flag = true;
  return function () {
    if (flag == true) {
      var context = this;
      var args = arguments;
      flag = false;
      isImmediate && fn.apply(context, args);
      setTimeout(() => {
        !isImmediate && fn.apply(context, args);
        flag = true;
      }, wait);
    }
  };
};
const common = {
  getMinuteTime,
  serializeQueryParams,
};
export default common;

// ps:
// 导入common对象
// import common from '@/api/common.js';
// 使用getMinuteTime方法
// let newTimeStr = common.getMinuteTime('2013/10/11 10:12:00');
// console.log(newTimeStr);
